﻿using System;
using System.Drawing;
using System.Windows.Forms;
using ProjectAutumn.Graphics;
using Color = ProjectAutumn.Graphics.Color;

namespace ProjectAutumn.GraphicsDevice.Form
{
    class FormGraphicsDevice : Graphics.GraphicsDevice
    {
        private System.Drawing.Graphics graphics;
        private readonly Control mainControl;
        private Bitmap image;
        private bool visibleState;
        private readonly System.Windows.Forms.Form mainForm;
        public FormGraphicsDevice(Game game)
            : base(game)
        {
            mainForm = (System.Windows.Forms.Form)Control.FromHandle(Game.WindowDevice.Handle);
            mainControl = new ChildControl { Size = new Size(game.WindowDevice.Width, game.WindowDevice.Height) };
            mainForm.Controls.Add(mainControl);
            image = new Bitmap(game.WindowDevice.Width, game.WindowDevice.Height);
            graphics = System.Drawing.Graphics.FromImage(image);
            mainControl.MouseEnter += delegate
            {
                if (visibleState)
                    Cursor.Show();
                else Cursor.Hide();
            };
            mainControl.MouseLeave += delegate
            {
                Cursor.Show();
            };
            mainControl.Paint += MainControlPaint;
        }
        void MainControlPaint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(image, Point.Empty);
        }
        public override void Clear(Color color)
        {
            graphics.Clear(ConvertColor(color));
        }
        protected override Texture LoadTexture(string file)
        {
            return new FormTexture(file);
        }
        protected override void BeginDraw() { }
        public override void Begin2D() { }
        public override void End2D() { }
        public override void Draw(Texture texture, Vector2 position, Rectangle? source, Color tint, float rotation, Vector2 offset, float scale)
        {
            if (source != null) throw new NotImplementedException("Form Graphics Device doesn't yet support having a non null source.");
            if (rotation != 0) throw new NotImplementedException("Form Graphics Device doesn't yet support rotation.");
            if (scale != 1) throw new NotImplementedException("Form Graphics Device doesn't yet support scaling.");
            if (tint != Color.White) throw new NotImplementedException("Form Graphics Device doesn't yet tinting.");
            graphics.DrawImage(((FormTexture)texture).Internal, new Point((int)(position.X - offset.X), (int)(position.Y - offset.Y)));
        }
        protected override void EndDraw()
        {
            mainControl.Invalidate();
        }
        public override int Width
        {
            get { return mainControl.Width; }
        }
        public override int Height
        {
            get { return mainControl.Height; }
        }
        public override Texture CreateTexture(int width, int height)
        {
            return new FormTexture(width, height);
        }
        protected override void SetMouseVisibility(bool value)
        {
            visibleState = value;
        }
        protected override void ApplyChanges(int width, int height, bool isFullscreen)
        {
            if (isFullscreen) throw new NotImplementedException("Form Graphics Device cannot go fullscreen.");
            image = new Bitmap(Game.WindowDevice.Width, Game.WindowDevice.Height);
            graphics = System.Drawing.Graphics.FromImage(image);
            mainControl.Width = width;
            mainControl.Height = height;
        }
        public override GraphicCapabilities GraphicCapabilities
        {
            get
            {
                return new GraphicCapabilities
                {
                        SupportsChangingMouseVisibility = true,
                        SupportsClearing = true,
                        SupportsDrawing2D = true,
                        SupportsDrawingRotation = false,
                        SupportsDrawingScale = false,
                        SupportsDrawingSource = false,
                        SupportsDrawingTint = false,
                        SupportsDynamicTextures = true,
                        SupportsFullscreen = false,
                        SupportsLoadingTextureFile = true,
                        SupportsTextureSetData = true,
                };
            }
        }
        public override void Dispose()
        {
            graphics.Dispose();
            image.Dispose();
        }
    }
}
